package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.AuthInfoDO;
import com.yuantu.judiciary.model.enums.AuthType;
import com.yuantu.judiciary.model.enums.OriginType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;

/**
 * 认证信息
 *
 * @author syw
 * @since 2023-08-16
 **/
public interface IAuthInfoDao extends JpaRepository<AuthInfoDO, Long> {

    AuthInfoDO findAuthInfoByIdAndDeletedFalse(Long id);

    @Modifying
    @Query("update AuthInfoDO set deleted = true where relativeId = :relativeId and type = :type and originType = :originType and deleted = false ")
    void deleteByRelativeId(Long relativeId, AuthType type, OriginType originType);

    AuthInfoDO findFirstByRelativeIdAndTypeAndOriginTypeAndExpiresTimeGreaterThanEqualAndDeletedFalseOrderByCreateTimeDesc(Long relativeId, AuthType type, OriginType originType, Timestamp expiresTime);

    AuthInfoDO findFirstByRelativeIdAndTypeOrderByCreateTimeDesc(Long id, AuthType authType);
}
